//
//  NearByCellClickDetailVC.m
//  VTravel
//
//  Created by lanouhn on 16/8/6.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "NearByCellClickDetailVC.h"

#import "NearByCellClickDetailModel.h"

#import "NearByCellClickDetailTableViewCell.h"

@interface NearByCellClickDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *rootTableView;
@property (nonatomic, strong) NSMutableArray *nearbyDataArray;
@property (nonatomic, strong) NSMutableArray *travelDataArray;

@property (nonatomic, strong) NSMutableArray *pickDataArray;


@end

@implementation NearByCellClickDetailVC

#pragma mark - 请求数据1
- (void)requestPOSTData{
    [NetWorkManager requestForPOSTWithURL:KNearByCellClickDetailURL paramater:@{@"city_id":city_id,@"route_type":@"-11", @"title":self.TitleStr} success:^(id responseObject) {
        if ([responseObject[@"status"] compare:@(1)] == NSOrderedSame) {
            for (NSDictionary *dic in responseObject[@"data"]) {
                NearByCellClickDetailModel *model = [[NearByCellClickDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.nearbyDataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.rootTableView reloadData];
            });
        }
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}
#pragma mark - 请求数据2
- (void)requestPOSTData2{
    [NetWorkManager requestForPOSTWithURL:KPickDesCellClickDetailURL paramater:@{@"search_type":@"1", @"title":self.TitleStr} success:^(id responseObject) {
        if ([responseObject[@"status"] compare:@(1)] == NSOrderedSame) {
            for (NSDictionary *dic in responseObject[@"data"]) {
                NearByCellClickDetailModel *model = [[NearByCellClickDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.pickDataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.rootTableView reloadData];
            });
        }
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}
#pragma mark - 请求数据3
- (void)requestGETData3{
    [NetWorkManager requestForGETWithURL:KTravelClickDetailURL paramater:@{@"city_id":city_id,@"route_type":self.route_type} success:^(id responseObject) {
        if([responseObject[@"status"] compare:@(1)] == NSOrderedSame){
            for (NSDictionary *dic in responseObject[@"data"]) {
                NearByCellClickDetailModel *model = [[NearByCellClickDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.travelDataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.rootTableView reloadData];
            });
        }
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nearbyDataArray = [NSMutableArray array];
    self.pickDataArray = [NSMutableArray array];
    self.travelDataArray = [NSMutableArray array];
    
    // 设置cell的自适应高度
    self.rootTableView.estimatedRowHeight = 100;
    self.rootTableView.rowHeight = UITableViewAutomaticDimension;
    self.title = self.TitleStr;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if (self.type == XYPushTransitionTypeFromRootNearByCells) {
            [self requestPOSTData];
        }else if(self.type == XYPushTransitionTypeFromRootPickerDestinationCells){
            [self requestPOSTData2];
        }else{
            [self requestGETData3];
        }
    });
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.type == XYPushTransitionTypeFromRootNearByCells) {
        return self.nearbyDataArray.count;
    }else if (self.type == XYPushTransitionTypeFromRootPickerDestinationCells){
        return self.pickDataArray.count;
    }else if(self.type == XYPushTransitionTypeFromRootTravelButtonCells){
        return self.travelDataArray.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 重用
    static NSString *identifier = @"CELL";
    
    NearByCellClickDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell  == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"NearByCellClickDetailTableViewCell" owner:self options:nil] lastObject];
    }
    if (self.type == XYPushTransitionTypeFromRootNearByCells) {
        if (self.nearbyDataArray.count != 0) {
            cell.model = self.nearbyDataArray[indexPath.row];
        }
    }else if (self.type == XYPushTransitionTypeFromRootPickerDestinationCells){
        if (self.pickDataArray.count != 0) {
            cell.model = self.pickDataArray[indexPath.row];
        }
    }else if (self.type == XYPushTransitionTypeFromRootTravelButtonCells){
        if (self.travelDataArray.count != 0) {
            cell.model = self.travelDataArray[indexPath.row];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tag == 0) {
        kMyLog(@"%@", self.nearbyDataArray[indexPath.row]);
    }else if (self.tag == 1){
        kMyLog(@"%@", self.pickDataArray[indexPath.row]);
    }else if (self.tag == 2){
        kMyLog(@"%@", self.travelDataArray[indexPath.row]);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

@end
